//
//  ViewController.m
//  engineeringDrawing
//
//  Created by BJB0004 on 17/4/1.
//  Copyright © 2017年 HangzhouBaixiongtechnologyco.,LTD. All rights reserved.
//

#import "ViewController.h"

#import "myView.h"
#import "DbanView.h"

#import "ImageViewController.h"

#define BWITH 16.85
#define LHEIGHT 121.7
#define HEIGHT 60.1

#define YBWITH 1685
#define YLHEIGHT 12170
#define YHEIGHT 6010
@interface ViewController (){
     BOOL isJ0;
     BOOL isJ1;
     BOOL isJ2;
    
    BOOL isOu0;
    BOOL isOu1;
    BOOL isOu2;


}
@property (strong, nonatomic) IBOutlet UITextField *heightField;
@property (strong, nonatomic) IBOutlet UITextField *weightField;
@property (strong, nonatomic) IBOutlet UILabel *Longlable;
@property (strong, nonatomic) IBOutlet UILabel *ShortLable;
@property (strong, nonatomic) IBOutlet UITextView *textView;
@property (nonatomic, strong) myView *myView ;
@property (nonatomic, strong) DbanView *DView ;
@property (nonatomic, strong) UIImage *myImage ;
@property (nonatomic, assign) double w;
@property (nonatomic, assign) double h;
@property (nonatomic, assign) int  redM;//红加密线
@property (nonatomic, assign) int  blueM;
@property (nonatomic, assign) int  redSM;//红双加密
@property (nonatomic, assign) int  blueSM;
@property (nonatomic, assign) int  redD;//红单
@property (nonatomic, assign) int  blueD;
@property (nonatomic, assign) int  redS;//红双
@property (nonatomic, assign) int  blueS;



@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.textView.editable = NO;
    
//   self.view.backgroundColor =[UIColor grayColor];
    
    
    // Do any additional setup after loading the view, typically from a nib.
}
- (IBAction)createDBView:(UIButton *)sender {
    if (self.DView) {
        self.DView = nil;
    }
    [self creatDView];
    
    
    self.myImage = [self makeImageSize:self.DView.bounds.size andView:self.DView];
    
    ImageViewController * image =[[ImageViewController alloc]init];
    
    image.myImage = self.myImage;
    image.w =self.w;
    image.h =self.h;
    
    
    
    //    [self presentViewController];
    [self presentViewController:image animated:YES completion:nil];

}
- (IBAction)creatMYview:(UIButton *)sender {
    if (self.myView) {
        self.myView = nil;
    }
    [self creatmyView];
    
    
    self.myImage = [self makeImageSize:self.myView.bounds.size andView:self.myView];
    
    ImageViewController * image =[[ImageViewController alloc]init];
    
    image.myImage = self.myImage;
    image.w =self.w;
    image.h =self.h;
    
    
    
    //    [self presentViewController];
    [self presentViewController:image animated:YES completion:nil];
}
- (IBAction)clearimageView:(UIButton *)sender {
   
   [self numDB];
    
    [self numLine];
    
}

/**
 计算线的种类和长度
 */
-(void)numLine{
    
    int  ww =[self.weightField.text intValue];
    int  hh =[self.heightField.text intValue];
    //    self.w = ww >hh ?hh :ww;
    //    self.h = ww <=hh ?hh :ww;
    
    
    self.w = (double)(ww/10.0);
    self.h = (double)(hh/10.0) ;
    
    self.redS = 0;
    self.redM = 0;
    self.redD = 0;
    self.redSM = 0;
    self.blueSM = 0;
    self.blueS = 0;
    self.blueD = 0;
    self.blueM = 0;
    
    int le1 = self.w/LHEIGHT;
    int num = 0;
    
    self.redM = 1;
    if (isJ0) {
        num = le1-1;
        if (le1%2==1) {
            self.blueS +=1;
        }else{
            self.redS  +=1;
        }
        
    }else{
        num = le1;
    }
    
    for (int i = 1; i<=num; i++) {
        if (i%2==1) {
         self.blueS +=2;
        }else{
        
        self.redS  +=2;
        }
    }
    
    
    if (isJ0&&isOu1) {
        
        self.redD +=1;
        self.blueD = 1;
        
        
    }
    
    
   else if (isJ1&&isOu0) {
       if (le1!=0) {
    
       if (le1%2==1) {
          self.redM += 1;
       }else{
        self.blueM += 1;
       }
       }else{
        self.blueM += 1;
       }
       
    }
    
   else if (isJ2&&isOu1) {
       if (le1!=0) {
           
        if (le1%2==1) {
            self.redSM +=1;
            self.blueM +=1;
            
        }else{
            self.blueSM +=1;
            self.redM +=1;
        }
       }else{
           self.redM +=1;
           self.blueSM +=1;
       
       }
    }
    
   else if (isJ0&&isOu2) {
        if (le1%2==1) {
            self.blueD +=2;
            self.redS +=1;
        }else{
        
            self.redD +=2;
            self.blueS +=1;
        
        }
    }
    
    
    NSMutableString * strM =[NSMutableString stringWithCapacity:20];
    
    
    [strM appendString:[NSString stringWithFormat:@"红主线根数:1 总长度:%.2fm\n",(self.w+140)/100.0]];
    [strM appendString:[NSString stringWithFormat:@"黑主线根数:1 总长度:%.2fm\n",(self.w+140)/100.0]];
    if (self.redM!=0) {
        [strM appendString:[NSString stringWithFormat:@"红加密线根数:%d 总长度:%.2fm\n",self.redM,self.redM*(self.h+30)/100.0]];
    }
    if (self.blueM!=0) {
        [strM appendString:[NSString stringWithFormat:@"黑加密线根数:%d 总长度:%.2fm\n",self.blueM,self.blueM*(self.h+30)/100.0]];
    }
    if (self.blueS!=0) {
        [strM appendString:[NSString stringWithFormat:@"黑双头线根数:%d 总长度:%.2fm\n",self.blueS,self.blueS*(self.h+30)/100.0]];
    }
    if (self.redS!=0) {
        [strM appendString:[NSString stringWithFormat:@"红双头线根数:%d 总长度:%.2fm\n",self.redS,self.redS*(self.h+30)/100.0]];
    }
    if (self.blueD!=0) {
        [strM appendString:[NSString stringWithFormat:@"黑单头线根数:%d 总长度:%.2fm\n",self.blueD,self.blueD*(self.h+30)/100.0]];
    }
    if (self.redD!=0) {
        [strM appendString:[NSString stringWithFormat:@"红单头线根数:%d 总长度:%.2fm\n",self.redD,self.redD*(self.h+30)/100.0]];
    }
    
    if (self.redSM!=0) {
        [strM appendString:[NSString stringWithFormat:@"红双头加密线:%d根 总长度:%.2fm\n",self.redSM,self.redSM*(self.h+30)/100.0]];
    }
    if (self.blueSM!=0) {
        [strM appendString:[NSString stringWithFormat:@"黑双头加密线:%d根 总长度:%.2fm\n",self.blueSM,self.blueSM*(self.h+30)/100.0]];
    }
    
   
    
    self.textView.text = strM;
    
    
    
    
}
//生成电路图
-(void)creatmyView{
    
    
    [self.view endEditing:YES];
    int  ww =[self.weightField.text intValue];
    int  hh =[self.heightField.text intValue];
//    self.w = ww >hh ?hh :ww;
//    self.h = ww <=hh ?hh :ww;
  
    
    self.w = (double)(ww/10.0);
    self.h = (double)(hh/10.0) ;
    
    self.myView =[[myView alloc]initWithFrame:CGRectMake(10, 120,self.w , self.h+40) andWith:ww and:hh];
    
//    [self.view addSubview:self.myView];


}

/**
 生成地板铺设图
 */
-(void)creatDView{
    
    [self.view endEditing:YES];
    int  ww =[self.weightField.text intValue];
    int  hh =[self.heightField.text intValue];
    //    self.w = ww >hh ?hh :ww;
    //    self.h = ww <=hh ?hh :ww;
    
    
    self.w = (double)(ww/10.0);
    self.h = (double)(hh/10.0) ;
    
    self.DView =[[DbanView alloc]initWithFrame:CGRectMake(10, 120,self.w , self.h) andWith:ww and:hh];

}


/**
 计算地板块数
 */
-(void)numDB{
    
    double weightField =  [self.weightField.text floatValue];
    double heightField = [self.heightField.text doubleValue];
    
   int ww = (int)(weightField*10.0);
    int hh = (int)(heightField*10.0) ;
    
    
    int duan=0 ;
    int  chang=0;
    
    int hang = hh/YBWITH;
    double hangyu = (double)((int)(hh)%YBWITH)*0.01;
    if (hangyu>0) {
        hang =hang +1;
    }
    
    int le1 = ww/YLHEIGHT;
    int le2 = (ww-YHEIGHT)/YLHEIGHT;
   
   
    double leYu1 =(double)(((int)(ww)) %YLHEIGHT)*0.01;
    
    
    double leYu2 =(double)((((int)(ww)) - YHEIGHT)%YLHEIGHT)*0.01;
    double duanY=0;
    
    
    for (int i= 1; i<=hang; i++) {
        if (i%2==1) {
            chang= chang+le1;
            
            if (leYu1<HEIGHT) {//短板切
                isJ0 = YES;
                isJ1 = NO;
                isJ2 = NO;
                if (duanY>=leYu1) {
                    duanY = duanY -leYu1;
                }else{
                duan = duan + 1;
                
                duanY = HEIGHT -leYu1;
                }
            }else if (leYu1>=HEIGHT&&leYu1<HEIGHT*2){//一块短板 + 一块短板有余数
                isJ0 = NO;
                isJ1 = YES;
                isJ2 = NO;
                duan = duan + 1;
//                duanY = HEIGHT*2 -leYu1;
                
                if (duanY>=leYu1-HEIGHT) {
                    duanY = duanY+HEIGHT -leYu1;
                }else{
                    duan = duan + 1;
                    
                    duanY = HEIGHT -(leYu1-HEIGHT);
                }
                
            }else{//两块短板

                isJ0 = NO;
                isJ1 = NO;
                isJ2 = YES;
                duan = duan + 2;
                

            }
        }else{
            chang = chang + le2;
            duan= duan +1;
        
            if (leYu2<HEIGHT) {//短板切
                isOu0 = YES;
                isOu1 = NO;
                isOu2 = NO;
                if (duanY>=leYu2) {
                    duanY = duanY - leYu2;
                }else{
                duan = duan + 1;
                
                duanY = HEIGHT -leYu2;
                }
            }else if (leYu2>=HEIGHT&&leYu2<HEIGHT*2){//一块短板 + 一块短板有余数
                isOu0 = NO;
                isOu1 = YES;
                isOu2 = NO;
                duan = duan + 1;
                
                if (duanY>=leYu2-HEIGHT) {
                    duanY = duanY - (leYu2-HEIGHT);
                }else{
                    duan = duan + 1;
                    
                    duanY = HEIGHT -(leYu2-HEIGHT);
                }

            }else{//两块短板
                isOu0 = NO;
                isOu1 = NO;
                isOu2 = YES;
               duan = duan + 2;
                
            }

        }
        
    }
    
    
    NSLog(@"%d=====%d ",duan,chang);
    
    self.Longlable.text = [NSString stringWithFormat:@"%d",chang];
    
    self.ShortLable.text = [NSString stringWithFormat:@"%d",duan];


}
- (UIImage *)makeImageSize:(CGSize)size andView:(UIView*)view
{
    
    // 下面方法，第一个参数表示区域大小。第二个参数表示是否是非透明的。如果需要显示半透明效果，需要传NO，否则传YES。第三个参数就是屏幕密度了，关键就是第三个参数 [UIScreen mainScreen].scale。
    UIGraphicsBeginImageContextWithOptions(size, NO, 0.0);
    [view.layer renderInContext:UIGraphicsGetCurrentContext()];
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    UIGraphicsEndImageContext();
    return image;
    
}

-(void)touchesBegan:(NSSet<UITouch *> *)touches withEvent:(UIEvent *)event{

    [self.view endEditing:YES];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
